﻿//
// File:    AppController.cs
// Project: MP3 Art Embedder v0.2.x
//
// Copyright 2011-2012 Kasey Osborn (Kasewick@gmail.com)
// Ms-PL license - Use and redistribute freely
//

using System;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;
using Mp3LibraryViewed;
using Mp3Library;

namespace WinController
{
    internal interface IAppViewFactory
    {
        void Make (AppController controller, Mp3LibraryModelBase modelBase);
    }


    /// <summary>
    /// WinForms Controller component of the MVC pattern that defines the user
    /// interface, processes user input, and dispatches that input to the model.
    /// </summary>
    internal partial class AppController : Form
    {
        private string[] args;
        private IAppViewFactory viewFactory;
        private Mp3LibraryModel model;


        public AppController (string[] args, IAppViewFactory viewFactory)
        {
            this.args = args;
            this.viewFactory = viewFactory;
            InitializeComponent();
        }


        protected override void Dispose (bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
                model.Dispose();
            }
            base.Dispose (disposing);
        }


        private static string GetProductAndVersionText ()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();

            string text = assembly.GetName().Version.ToString();
            if (text.Length > 3 && text.EndsWith (".0"))
                text = text.Substring (0, text.Length - 2);

            object[] atts = assembly.GetCustomAttributes (typeof (AssemblyProductAttribute), false);
            if (atts.Length > 0)
                text = ((AssemblyProductAttribute) atts[0]).Product + " v" + text;

            return text;
        }


        private void AppController_Shown (object sender, EventArgs e)
        {
            Text = GetProductAndVersionText();

            // Sync UI with command line.
            if (args.Length > 0)
                rootNameTextBox.Text = args[0];

            // Create the Model.
            model = new Mp3LibraryModel();

            // Create the View and connect to the Model.
            viewFactory.Make (this, model);

            // Sync UI with Model.
            logBox.IsMultiline = model.IsVerbose;
            fileNamesTextBox.Text = model.GetArtFileNames();
            model.ShowHelp();
        }


        private void browseButton_Click (object sender, EventArgs e)
        {
            using (var dialog = new FolderBrowserDialog())
            {
                if (!string.IsNullOrWhiteSpace (rootNameTextBox.Text))
                    dialog.SelectedPath = rootNameTextBox.Text;

                var result = dialog.ShowDialog();

                if (result == DialogResult.OK)
                    rootNameTextBox.Text = dialog.SelectedPath;
            }
        }


        private void limitSizeCheckBox_CheckedChanged (object sender, EventArgs e)
        {
            maxSizeUpDown.Enabled = limitSizeCheckBox.Checked;
        }


        private void verboseCheckBox_CheckedChanged (object sender, EventArgs e)
        {
            model.IsVerbose = verboseCheckBox.CheckState == CheckState.Checked;
        }


        private void updateButton_Click (object sender, EventArgs e)
        {
            var mode = UpdateMode.Default;

            if (replaceCheckBox.Checked)
                mode |= UpdateMode.ReplaceExisting;

            if (whatifCheckBox.Checked)
                mode |= UpdateMode.WhatIf;

            model.ClearArtFileNames();
            model.AddArtFileNames (fileNamesTextBox.Text);

            model.UpdateCoversAsync (mode, rootNameTextBox.Text, (int) maxSizeUpDown.Value);
        }


        private void cancelButton_Click (object sender, EventArgs e)
        {
            model.Cancel();
        }
    }
}
